package com.ccbooks.view;

import java.util.List;

import com.ccbooks.adapter.CatelogAdapter;
import com.ccbooks.bo.BookMarkBo;
import com.ccbooks.bo.BooksBo;
import com.ccbooks.listener.BtnBookStoreClick;
import com.ccbooks.listener.BtnCatalogueClick;
import com.ccbooks.listener.BtnContentCatelogClick;
import com.ccbooks.listener.BtnContentMarkClick;
import com.ccbooks.listener.bookCatalog.LvCatalogOnItemClickListener;
import com.ccbooks.vo.Book;
import com.ccbooks.vo.CatelogItem;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class BookCatalogView extends Activity {
	
	// 所有控件；
	private Button btnBookStore;
	private Button btnCatalogue;
	private Button btnLight;
	private Button btnFont;
	private Button btnSearch;
	public Button btnBookMark;
	private LinearLayout bookpagebg; // 竖屏书背景；

	private TextView tvBookNameContent;
	// private LinearLayout llyContentMain;
	private LinearLayout llyContentMain;
	private LinearLayout llyCatelogTitle;
	private Button btnContentCatelog;
	public Button btnContentMark;
	private ListView lvCatalog;
	
	public short showCatelogType; // 目录显示规则 1表示目录，2表示书签；
	public short portraint; // 横竖屏显示 横屏为2，竖屏为1；	
	public Book book;
	public CatelogItem mark;
	private TextView tvEmpty; 
	/**items*/
	public List<CatelogItem> items;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Bundle bundle = this.getIntent().getExtras();
		int id = bundle.getInt("bookId");
		BooksBo bo = new BooksBo(this, false);
		book = bo.getOne(id);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // ����
			// doSomrthing
			portraint = 1;
			setContentView(R.layout.book_catalog);
			
		} else {
			// ����ʱdosomething
			portraint = 2;
			setContentView(R.layout.book_catalog_horizontal);
		}
		findview();
		addListener();
		doing();
	}
	
	private void findview() {
		bookpagebg = (LinearLayout) findViewById(R.id.bookpagebg);
		btnBookStore = (Button) findViewById(R.id.btnBookStore);
		btnCatalogue = (Button) findViewById(R.id.btnCatalogue);
		btnLight = (Button) findViewById(R.id.btnLight);
		btnFont = (Button) findViewById(R.id.btnFont);
		btnSearch = (Button) findViewById(R.id.btnSearch);
		btnBookMark = (Button) findViewById(R.id.btnBookMark);
		tvBookNameContent = (TextView) findViewById(R.id.tvBookNameContent); 
		llyContentMain = (LinearLayout) findViewById(R.id.llyContentMain);
		llyCatelogTitle = (LinearLayout) findViewById(R.id.llyCatelogTitle);
		btnContentCatelog = (Button) findViewById(R.id.btnContentCatelog);
		btnContentMark = (Button) findViewById(R.id.btnContentMark);
		lvCatalog = (ListView) findViewById(R.id.lvCatalog);
		tvEmpty  = (TextView) findViewById(android.R.id.empty);
		if (portraint == 1) {
			
		} else if (portraint == 2) {
			
		}
	}

	private void addListener() {
		
		BtnCatalogueClick btnCatalogueClick = new BtnCatalogueClick(this,book.id,false);
		btnCatalogue.setOnClickListener(btnCatalogueClick);
		
		BtnContentCatelogClick btnContentCatelogClick = new BtnContentCatelogClick(
				this,btnContentCatelog,btnContentMark,lvCatalog );
		btnContentCatelog.setOnClickListener(btnContentCatelogClick);
		
		BtnContentMarkClick btnContentMarkClick = new BtnContentMarkClick(
				this,btnContentCatelog, btnContentMark);
		btnContentMark.setOnClickListener(btnContentMarkClick);
		
		BtnBookStoreClick btnBookStoreClick = new BtnBookStoreClick(this);
		btnBookStore.setOnClickListener(btnBookStoreClick);
	
		lvCatalog .setOnItemClickListener( new LvCatalogOnItemClickListener(this));
	}

	private void doing() {
		showCatelogType = 1;
		showing();

	}

	public void showing() {
		btnCatalogue.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_transparent));
		btnCatalogue.setText("续读");
		tvBookNameContent.setText(book.bookname);
		btnLight.setBackgroundResource(R.drawable.content_btn_light_lnvalid);
		btnFont.setBackgroundResource(R.drawable.content_btn_font_lnvalid);
		btnBookMark
				.setBackgroundResource(R.drawable.content_btn_outmark_lnvalid);
		if (portraint == 1) {
			
			
		} else if (portraint == 2) {
		
			
		}
		if(showCatelogType == 1){
			showListCatelog();	
		}else if (showCatelogType ==2){
			showListMark();	
		}
		 
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
		{   
			// TODO When the screen is PORTRAIT
			portraint = 1;
			System.out.println("onConfigurationChanged(===================");
			System.out.println("totalMemory===================>"+Runtime.getRuntime().totalMemory());
			System.out.println("freeMemory===================>"+Runtime.getRuntime().freeMemory());
			setContentView(R.layout.book_catalog);
		}

		if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
		{
			// TODO When the screen is LANDSCAPE
			portraint = 2;
			System.out.println("onConfigurationChanged(===================");
			System.out.println("totalMemory===================>"+Runtime.getRuntime().totalMemory());
			System.out.println("freeMemory===================>"+Runtime.getRuntime().freeMemory());
			setContentView(R.layout.book_catalog_horizontal);
		}
		findview();
		addListener();
		doing();
		super.onConfigurationChanged(newConfig);
		System.out.println("onConfigurationChanged(===================");
		System.out.println("totalMemory===================>"+Runtime.getRuntime().totalMemory());
		System.out.println("freeMemory===================>"+Runtime.getRuntime().freeMemory());
	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}
	
	/**显示目录列表项*/
	private void showListCatelog() {
		CatelogAdapter adapter = new CatelogAdapter(this, null);
		lvCatalog.setAdapter(adapter);
		tvEmpty.setText("本书没有目录项");
		lvCatalog.setEmptyView(tvEmpty);
	}
	
	/**显示书签列表项*/
	private void showListMark() {
		BookMarkBo bo = new BookMarkBo(this);
		items= bo.readMarksByDb(book.id);
		CatelogAdapter adapter = new CatelogAdapter(this, items);
		lvCatalog.setAdapter(adapter);
		tvEmpty.setText("本书现在未有书签");
		lvCatalog.setEmptyView(tvEmpty);
	}
}
